﻿using System.Globalization;
using System.Windows;
using System.Windows.Media;
namespace CoreModule
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static class Extension
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetClientId()
        {
            var clientId= string.Join(",", CoreModule.Computer.GetMacList().ToList());  
            if (clientId.Length>100)
            {
                return clientId.Substring(0, 100);
            }
            else
            {
                return clientId;
            }
        }

        //public static List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
        //{
        //    DependencyObject child = null;
        //    List<T> childList = new List<T>();

        //    for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
        //    {
        //        child = VisualTreeHelper.GetChild(obj, i);

        //        if (child is T && (((T)child).GetType() == typename))
        //        {
        //            childList.Add((T)child);
        //        }
        //        childList.AddRange(GetChildObjects<T>(child, typename));
        //    }
        //    return childList;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>


        public static Window GetWindow(this FrameworkElement element)
        {
            // 获取可视树中的上一级
            DependencyObject parent = VisualTreeHelper.GetParent(element);

            // 如果父级是null，则直接返回null
            if (parent == null) return null;

            // 如果父级已经是Window，则直接返回
            if (parent is Window window)
                return window;
            else
                // 否则递归查找
                return GetWindow((FrameworkElement)parent);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static List<T> GetChildObjects<T>(DependencyObject obj, Type typename, bool recursion = true) where T : FrameworkElement
        {


            DependencyObject child = null;
            List<T> childList = new List<T>();

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child is T && (((T)child).GetType() == typename))
                {
                    childList.Add((T)child);
                }
                if (recursion)
                {
                    childList.AddRange(GetChildObjects<T>(child, typename));
                }
            }
            return childList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PulseRate"></param>
        /// <returns></returns>
        public static PulseRateStatus ToPulseRateStatus(int pulseRate)
        {
            if (pulseRate == 0)
            {
                return PulseRateStatus.None;
            }
            else if (pulseRate > 100)
            {
                return PulseRateStatus.High;
            }
            else if (pulseRate >= 60)
            {
                return PulseRateStatus.Normal;
            }
            else
            {
                return PulseRateStatus.Low;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        static int Underage_High_Value { get; set; } = 18;
        /// <summary>
        /// 
        /// </summary>
        static int Youth_High_Value { get; set; } = 36;
        /// <summary>
        /// 
        /// </summary>
        static int Middle_High_Value { get; set; } = 60;

        /// <summary>
        ///  年龄分段标准
        /// </summary>
        /// <param name="age"></param>
        /// <returns></returns>

        public static AgeGroup ToAgeGroup(int? age)
        {
            if (!age.HasValue)
            {
                return AgeGroup.None;
            }
            else if (age == 0)
            {
                return AgeGroup.None;
            }
            else if (age < Underage_High_Value)
            {
                return AgeGroup.Underage;
            }
            else if (age < Youth_High_Value)
            {
                return AgeGroup.Youth;
            }
            else if (age < Middle_High_Value)
            {
                return AgeGroup.Middle;
            }
            return AgeGroup.Old;
        }
        /// <summary>
        /// 血流灌注指数
        /// </summary>
        /// <param name="perfusionIndex"></param>
        /// <returns></returns>
        public static PerfusionIndexStatus ToPerfusionIndexStatus(float perfusionIndex)
        {
            if (perfusionIndex == 0.0)
            {
                return PerfusionIndexStatus.None;
            }
            else if (perfusionIndex < 1.4)
            {
                return PerfusionIndexStatus.Low;
            }
            else if (perfusionIndex >= 1.4
                && perfusionIndex <= 10)
            {
                return PerfusionIndexStatus.Normal;
            }
            else
            {
                return PerfusionIndexStatus.High;
            }

        }

        /// <summary>
        /// 体温
        /// </summary>
        /// <param name="bloodOxygen"></param>
        /// <returns></returns>
        public static TemperatureStatus ToTemperatureStatus(float temperature)
        {
            if (temperature == 0)
            {
                return TemperatureStatus.None;
            }
            else if (temperature < 32.0)
            {
                return TemperatureStatus.Low;
            }
            else if (temperature >= 32.0
                && temperature <= 37.50)
            {
                return TemperatureStatus.Normal;
            }
            else if (temperature >= 37.60
               && temperature <= 43.0)
            {
                return TemperatureStatus.High;
            }
            else
            {
                return TemperatureStatus.Hi;
            }
        }
        /// <summary>
        /// 压力等级
        /// </summary>
        /// <param name="bloodOxygen"></param>
        /// <returns></returns>
        public static PressureLevel ToPressureLevel(int sBP, int dBP)
        {

            if (sBP < 90 && dBP < 60)
            {
                return PressureLevel.None;
            }

            if ((dBP >= 60 && dBP < 90)
                && (sBP >= 90 && sBP < 140))
            {
                return PressureLevel.Normal;
            }

            if (sBP >= 90 && sBP < 140)
            {
                if (dBP < 60)
                {
                    return PressureLevel.Lower;
                }

                if (dBP >= 90 && dBP < 100)
                {
                    return PressureLevel.High1;
                }

                if (dBP >= 100 && dBP < 110)
                {
                    return PressureLevel.High2;
                }
                if (dBP >= 110)
                {
                    return PressureLevel.High3;
                }
            }

            if (dBP >= 60 && dBP < 90)
            {
                if (sBP >= 140 && sBP < 160)
                {
                    return PressureLevel.High1;
                }
                if (sBP >= 160 && sBP < 180)
                {
                    return PressureLevel.High2;
                }
                if (sBP >= 180)
                {
                    return PressureLevel.High3;
                }
            }


            if (sBP >= 140 && dBP < 90)
            {
                return PressureLevel.JustHigh;
            }

            if (sBP >= 180 || dBP >= 110)
            {
                return PressureLevel.High3;
            }

            if (sBP >= 160 || dBP >= 100)
            {
                return PressureLevel.High2;
            }
            if (sBP >= 140 || dBP >= 90)
            {
                return PressureLevel.High1;
            }
            return PressureLevel.None;
        }

        /// <summary>
        /// 血氧状态
        /// </summary>
        /// <param name="DBP"></param>
        /// <returns></returns>
        public static BloodOxygenStatus ToBloodOxygenStatus(float bloodOxygen)
        {
            if (bloodOxygen == 0.0)
            {
                return BloodOxygenStatus.None;
            }
            else if (bloodOxygen < 95.0)
            {
                return BloodOxygenStatus.Low;
            }
            else
            {
                return BloodOxygenStatus.Normal;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="DBP"></param>
        /// <returns></returns>
        public static PressureStatus ToDBPStatus(int DBP)
        {
            if (DBP == 0)
            {
                return PressureStatus.None;
            }
            else if (DBP >= 110)
            {
                return PressureStatus.High3;
            }
            else if (DBP >= 100)
            {
                return PressureStatus.High2;
            }
            else if (DBP >= 90)
            {
                return PressureStatus.High1;
            }
            else if (DBP >= 60)
            {
                return PressureStatus.Normal;
            }
            else
            {
                return PressureStatus.Low;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SBP"></param>
        /// <returns></returns>
        public static PressureStatus ToSBPStatus(int SBP)
        {
            if (SBP == 0)
            {
                return PressureStatus.None;
            }
            else if (SBP >= 180)
            {
                return PressureStatus.High3;
            }
            else if (SBP >= 160)
            {
                return PressureStatus.High2;
            }
            else if (SBP >= 140)
            {
                return PressureStatus.High1;
            }
            else if (SBP >= 90)
            {
                return PressureStatus.Normal;
            }
            else
            {
                return PressureStatus.Low;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sex"></param>
        /// <returns></returns>
        public static SexKind GetSexKind(string sex)
        {
            if (string.IsNullOrWhiteSpace(sex))
            {
                return SexKind.None;
            }
            else if (sex.Contains("1"))
            {
                return SexKind.Male;
            }
            else if (sex.Contains("2"))
            {
                return SexKind.Female;
            }
            else if (sex.ToLower().Contains("f"))
            {
                return SexKind.Female;
            }
            else if (sex.ToLower().Contains("m"))
            {
                return SexKind.Male;
            }
            else if (sex.Contains("男"))
            {
                return SexKind.Male;
            }
            else if (sex.Contains("女"))
            {
                return SexKind.Female;
            }
            else
            {
                return SexKind.None;
            }
        }
        /// <summary>
        /// 获取 年龄
        /// </summary>
        /// <param name="IdCard"></param>
        /// <param name="birthdayString"></param>
        /// <returns></returns>
        public static int GetAge(string IdCard, string birthdayString)
        {
            DateTime birthday = DateTime.Now;
            try
            {
                if (!string.IsNullOrEmpty(IdCard) && IdCard.Length >= 14
                   && DateTime.TryParseExact(IdCard.Substring(6, 8), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday))
                {
                    return (int)(DateTime.Now.Subtract(birthday).TotalDays / 365.25);
                }
                else if (!string.IsNullOrEmpty(birthdayString)
                    && DateTime.TryParseExact(birthdayString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday))
                {
                    return (int)(DateTime.Now.Subtract(birthday).TotalDays / 365.25);
                }
                else if (!string.IsNullOrEmpty(birthdayString)
                  && DateTime.TryParseExact(birthdayString, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday))
                {
                    return (int)(DateTime.Now.Subtract(birthday).TotalDays / 365.25);
                }
                else if (!string.IsNullOrEmpty(birthdayString)
                && DateTime.TryParseExact(birthdayString, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday))
                {
                    return (int)(DateTime.Now.Subtract(birthday).TotalDays / 365.25);
                }
                else if (!string.IsNullOrEmpty(birthdayString)
               && DateTime.TryParseExact(birthdayString, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday))
                {
                    return (int)(DateTime.Now.Subtract(birthday).TotalDays / 365.25);
                }
                else if (!string.IsNullOrEmpty(birthdayString)
               && DateTime.TryParseExact(birthdayString, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday))
                {
                    return (int)(DateTime.Now.Subtract(birthday).TotalDays / 365.25);
                }
            }
            catch (Exception)
            {
            }
            return 0;
        }
    }
}
